﻿using Common;
using EWPlatAPP.UControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWPlatAPP.Utility
{
    public static class FormHelper
    {
        /// <summary>
        /// 添加Form到TabPage中
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="frm"></param>
        public static void AddFormToPage(this TabControl tab, Form frm)
        {
            if (!tab.TabPages.ContainsKey(frm.Name))
            {
                frm.FormBorderStyle = FormBorderStyle.None;
                if(frm.GetType().BaseType==typeof(FormParent))
                {
                   ( (FormParent)frm).TopLevel = false;
                }
                else 
                    frm.TopLevel = false;
                frm.WindowState = FormWindowState.Maximized;
                TabPage page = new TabPage();
                frm.Parent = page;
                frm.Dock = DockStyle.Fill;
                page.Name = frm.Name;
                page.Text = frm.Text;
                page.AutoScroll = true;
                tab.TabPages.Add(page);
                tab.SelectedTab = page;
                frm.Show();
            }
            else
            {
                TabPage page = tab.TabPages[frm.Name];
                tab.SelectedTab = page;
            }
        }

        /// <summary>
        /// 将Form显示在Panel中
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="frm"></param>
        public static void AddFormToPanel(this Panel panel, Form frm)
        {
            bool bl = false;
            foreach (Control c in panel.Controls)
            {
                Form f = c as Form;
                if(f.Name==frm.Name)
                {
                    frm = f;
                    bl = true;
                }
                else
                {
                    f.Hide();
                }
            }
            if(!bl)
            {
                panel.Controls.Clear();
                panel.SuspendLayout();
                frm.TopLevel = false;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.WindowState = FormWindowState.Maximized;
                frm.Dock = DockStyle.Fill;
                panel.AutoScroll = true;
                panel.Controls.Add(frm);
                frm.Show();
                panel.ResumeLayout();
            }
            else
            {
                frm.Activate();
            }
      
        }


        /// <summary>
        /// 关闭指定的窗体
        /// </summary>
        /// <param name="formName"></param>
        public static void CloseOpenForm(string formName)
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == formName)
                {
                    frm.Close();
                    break;
                }
            }
        }
        /// <summary>
        /// 关闭指定页中的Form
        /// </summary>
        /// <param name="page"></param>
        public static void CloseOpenForm(TabPage page)
        {
            foreach (Control c in page.Controls)
            {
                 if(c is Form)
                {
                    Form f = c as Form;
                    f.Close();
                }
            }
        }

        /// <summary>
        /// TabControl Size属性变化时处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TabControl_SizeChanged(object sender, EventArgs e)
        {
            TabControl tab = sender as TabControl;
            if (tab.TabPages.Count > 0)
            {
                Size size = tab.SelectedTab.Size;
                foreach (TabPage page in tab.TabPages)
                {
                    foreach (Control c in page.Controls)
                    {
                        if (c is Form)
                        {
                            Form f = c as Form;
                            f.WindowState = FormWindowState.Normal;
                            f.SuspendLayout();
                            f.Size = size;
                            f.ResumeLayout(true);
                            f.WindowState = FormWindowState.Maximized;
                        }

                    }
                }
            }
        }

        /// <summary>
        /// 设置指定Panel中的按钮的文字颜色切换
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="selBtn"></param>
        /// <param name="selColor"></param>
        /// <param name="unSelColor"></param>
        public static  void SetSelectBtnFont(Panel panel, UCircleButton selBtn,Color selColor,Color unSelColor)
        {
            foreach (Control c in panel.Controls)
            {
                if (c is UCircleButton)
                {
                    UCircleButton btn = c as UCircleButton;
                    if (btn == selBtn)
                        btn.SelectedColor =selColor;
                    else
                        btn.SelectedColor = unSelColor;
                }
            }
        }

        /// <summary>
        /// 关闭按钮的显示处理
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="pic"></param>
        public static void ShowCloseBtn(TabControl tab,PictureBox pic)
        {
            if (tab.TabCount > 0)
                pic.Visible = true;
            else
            {
                pic.Visible = false;
                tab.TabPages.Clear();
            }
                
        }

        /// <summary>
        /// 打开指定类型的Form
        /// </summary>
        /// <typeparam name="F"></typeparam>
        public static  void ShowForm<F>(object obj)
        {
            Form frm = (Form)Activator.CreateInstance(typeof(F));
            if (obj != null)
                frm.Tag = obj;
            frm.Show();
        }

        /// <summary>
        /// 显示指定的Form,内嵌到TabControl还是Show
        /// </summary>
        /// <param name="fClassName"></param>
        /// <param name="obj"></param>
        /// <param name="isTop"></param>
        public static  void ShowForm(string fClassName, object obj,Form fParent)
        {
            Type type = Type.GetType(fClassName);
            if (type != null)
            {
                Form frm = (Form)Activator.CreateInstance(Type.GetType(fClassName));
                if (obj != null)
                    frm.Tag = obj;
                if (fParent != null)
                    frm.ShowDialog(fParent);
                else
                    frm.ShowDialog();
            }

        }

        /// <summary>
        /// 将Form添加到TabPage
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="fClassName"></param>
        /// <param name="obj"></param>
        public  static void AddFormToTabPage(TabControl tab, string fClassName, object obj)
        {
            Type type = Type.GetType(fClassName);
            if (type != null)
            {
                Form frm = (Form)Activator.CreateInstance(type);
                if (frm != null)
                {
                    if (obj != null)
                        frm.Tag = obj;
                    tab.AddFormToPage(frm);
                }
            }


        }

        /// <summary>
        /// 将Form添加到Panel
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="fClassName"></param>
        /// <param name="obj"></param>
        public static  void AddFormToPanel(Panel panel, string fClassName, object obj)
        {
            Type type = Type.GetType(fClassName);
            if (type != null)
            {
                Form frm = (Form)Activator.CreateInstance(type);
                if (frm != null)
                {
                    if (obj != null)
                        frm.Tag = obj;
                    panel.AddFormToPanel(frm);
                }
            }
        }

        /// <summary>
        /// 获取操作名称（删除、恢复、移除）
        /// </summary>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
       public static string GetDelTypeName(int isDeleted)
        {
            string delType = "删除";
            switch (isDeleted)
            {
                case 1:
                    delType = "删除";
                    break;
                case 0:
                    delType = "恢复";
                    break;
                case 2:
                    delType = "移除";
                    break;
            }
            return delType;
        }

        /// <summary>
        /// 刷新DataGridView  新增、修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dgv"></param>
        /// <param name="actType"></param>
        /// <param name="info"></param>
        public static void UpdateDgv<T>(this DataGridView dgv,int actType,T info,int id)
        {
            List<T> list = dgv.DataSource as List<T>;
            dgv.DataSource = null;
          if(actType ==1)//新增
            {
                list.Add(info);
            }
          else if(actType==2)//修改
            {
                int index = -1;
                Type type = typeof(T);
                string keyName = type.GetPrimaryName();
                foreach(T t in list)
                {
                    int uId = (int)type.GetProperty(keyName).GetValue(t);
                    if(uId==id)
                    {
                        index = list.IndexOf(t);
                        break;
                    }
                }
                list[index] = info;
            }
            dgv.DataSource = list;
        }

        /// <summary>
        /// 刷新DataGridView  删除、移除、恢复
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dgv"></param>
        /// <param name="isDeleted"></param>
        /// <param name="list0"></param>
        public static void UpdateDgv<T>(this DataGridView dgv,  List<T> list0)
        {
            List<T> list = dgv.DataSource as List<T>;
            dgv.DataSource = null;
            list0.ForEach(t => list.Remove(t));
            dgv.DataSource = list;
        }

        /// <summary>
        /// 设置页面按钮或复选框的可见性
        /// </summary>
        /// <param name="control"></param>
        /// <param name="reControls"></param>
        /// <param name="funs"></param>
        public static Dictionary<string,bool> SetBtnsVisible(this Control[] controls,Control[] reControls,List<string> funs)
        {
            Dictionary<string, bool> dicRight = new Dictionary<string, bool>();
            //针对Button或CheckBox
            foreach(Control c in controls)
            {
                if (c.GetType() == typeof(Button) || c.GetType() == typeof(CheckBox) || c.GetType().BaseType == typeof(Button))
                {
                    string conText = c.Text.Trim();
                    if (c.Text.Contains("+"))
                        conText = conText.Substring(1);
                    bool blExist = funs.Contains(conText);
                    c.Visible = blExist;
                    //针对新增、修改
                    if (conText == "新增" || conText == "修改")
                    {
                        foreach (Control cc in reControls)
                        {
                            cc.Visible = blExist;
                        }
                    }
                    dicRight.Add(c.Name, blExist);
                }
            }
            return dicRight;
           
        }

        /// <summary>
        /// 设置指定操作列的可见性
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="funName"></param>
        /// <param name="colName"></param>
        /// <param name="funs"></param>
        public static void SetDgvColsVisible(this DataGridView dgv,string funName,string colName, List<string> funs)
        {
            bool blExist = funs.Contains(funName);
            dgv.Columns[colName].Visible = blExist;
        }
    }
}
